#include "BlockQueue.hpp"
#include "Thread.hpp"
#include "Task.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
using namespace ThreadModel;


// using BlockQueue_t = BlockQueue<int>;

// void Productor(BlockQueue_t& bq)
// {
//     int cnt = 10;
//     while(true)
//     {
//         sleep(1);

//         bq.Enqueue(cnt);
//         std::cout << "Productor :" << cnt++ << std::endl;
//     }
// }

// void Consumer(BlockQueue_t& bq)
// {
//     while(true)
//     {
//         int t = 0;
//         bq.pop(&t);
//         std::cout << "Consumer : " << t << std::endl;
//     }
// }


// void StartCommon(std::vector<Thread<BlockQueue_t>>* threads, int num, func_t<BlockQueue_t> func, BlockQueue_t& bq)
// {
//     for (int i = 0; i < num; i++)
//     {
//         std::string name = "thread-" + std::to_string(i + 1);
//         threads->emplace_back(func, bq, name);
//         threads->back().Start();
//     }
// }

// void StartProduct(std::vector<Thread<BlockQueue_t>>* threads, int num, BlockQueue_t& bq)
// {
//     StartCommon(threads, num, Productor, bq);
// }

// void StartConsum(std::vector<Thread<BlockQueue_t>>* threads, int num, BlockQueue_t& bq)
// {
//     StartCommon(threads, num, Consumer, bq);
// }

// void WaitAllThread(std::vector<Thread<BlockQueue_t>>& threads)
// {
//     for (auto& thread : threads)
//     {
//         thread.Join();
//     }
// }

// int main()
// {
//     srand(time(nullptr));
//     BlockQueue_t* bq = new BlockQueue_t(5);
//     std::vector<Thread<BlockQueue_t>> threads;
//     StartProduct(&threads, 1, *bq);
//     StartConsum(&threads, 3, *bq);
//     WaitAllThread(threads);
//     return 0;
// }





int a = 10;
void Productor(int& data)
{
    while(data)
    {
        std::cout << "Productor :" << data-- << std::endl;
        sleep(1);
    }
}

void Consumer(int& data)
{
    while(data)
    {
        std::cout << "Consumer :" << data-- << std::endl;
        sleep(1);
    }
}


void StartProduct(std::vector<Thread<int>>& threads, int num)
{
    for (int i = 0; i < num; i++)
    {
        std::string name = "productor-" + std::to_string(i + 1);
        threads.emplace_back(Productor, a, name);
        threads.back().Start();
    }
}

void StartConsum(std::vector<Thread<int>>& threads, int num)
{
    for (int i = 0; i < num; i++)
    {
        std::string name = "consumer-" + std::to_string(i + 1);
        threads.emplace_back(Consumer, a, name);
        threads.back().Start();
    }
}

void WaitAllThread(std::vector<Thread<int>>& threads)
{
    for (auto& thread : threads)
    {
        thread.Join();
    }
}

int main()
{
    std::vector<Thread<int>> threads;
    StartProduct(threads, 1);
    StartConsum(threads, 1);
    WaitAllThread(threads);
    return 0;
}
